package com.nondo.dean.auth.controller.api;

import com.nondo.dean.auth.common.bean.AuthorityItem;
import com.nondo.dean.auth.domain.Authority;
import com.nondo.dean.auth.provider.AuthorityManager;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 权限对外接口
 *
 * @author lifeng
 * @since 2018/1/18
 */
@RestController
@RequestMapping("/{v}/authority")
public class AuthorityController {

    @Autowired
    private AuthorityManager authorityManager;

    /**
     * 权限注册
     *
     * @param authorityItem 权限参数
     * @return 注册消息
     */
    @ApiVersion(1)
    @RequestMapping(
            value = "/register",
            method = RequestMethod.POST
    )
    public View<String> requestAuthorityRegister(@Validated @RequestBody AuthorityItem authorityItem) {

        return authorityManager.registerAuthority(authorityItem);
    }

    /**
     * 权限删除
     *
     * @param authorityId 权限编号
     * @return 删除消息
     */
    @ApiVersion(1)
    @RequestMapping(
            value = "/one/{authorityId}",
            method = RequestMethod.POST
    )
    public View<Authority> requestAuthorityOne(@PathVariable Long authorityId) {

        return authorityManager.findAuthorityById(authorityId);
    }

    /**
     * 权限删除
     *
     * @param authorityId 权限编号
     * @return 删除消息
     */
    @ApiVersion(1)
    @RequestMapping(
            value = "/delete/{authorityId}",
            method = RequestMethod.POST
    )
    public View<String> requestAuthorityDelete(@PathVariable Long authorityId) {

        return authorityManager.deleteAuthority(authorityId);
    }

    /**
     * 分页查询权限
     *
     * @param query 查询参数
     * @return 分页结果
     */
    @ApiVersion(1)
    @RequestMapping(
            value = "/list",
            method = RequestMethod.POST
    )
    public View<Page<Authority>> requestAuthorityPage(@RequestBody Query<AuthorityItem> query) {

        return authorityManager.findPageAuthority(query);
    }


}
